package com.example.imitation_webchat_demo.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import android.app.Activity;

/**
 * @author ztq
 * 2015年6月6日下午5:33:28
 * @作用：
 */
public class ApplicationUtils {
	private List<Activity> mList;
	private static ApplicationUtils instance;
	private static Lock lock = new ReentrantLock();
	
	private ApplicationUtils(){
		mList = new ArrayList<Activity>();
	}
	
	public static ApplicationUtils getInstance(){
		if(instance == null){
			lock.lock();
			if(instance == null){
				instance = new ApplicationUtils();
			}
			lock.unlock();
		}
		return instance;
	}
	
	// add Activity  
    public void addActivity(Activity activity) { 
    	if(mList != null){
    		mList.add(activity);
    	}
    }
    
    public void removeActivity(Activity activity){
    	if(mList.size() != 0){
    		mList.remove(activity);
    	}
    }
    
    // 退出方法
    public void exit() { 
        try { 
        	BPUtils.getInstance().clearBitmap();   // 将bitmpa对象从内存中回收
            for (Activity activity : mList) { 
                if (activity != null) 
                    activity.finish();       // 将所有页面销毁
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            System.exit(0); 
        } 
    }
    
    public void removeActivity(String activityName){
    	for(Activity activity: mList){
    		String className = activity.getClass().getName();
    		if(className.contains(activityName)){
    			activity.finish();
    			break;
    		}
    	}
    }
}